<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let data = [
            {
                "id": 1,
                "label": "菜单1",
                "children": [
                    {
                        "id": 2,
                        "label": "菜单1-1",
                        "children": [
                            {
                                "id": 3,
                                "label": "菜单1-1-1",
                            },
                            {
                                "id": 4,
                                "label": "菜单1-1-2",
                            }
                        ],
                    },
                    {
                        "id": 5,
                        "label": "菜单1-2",
                        "children": [
                            {
                                "id": 6,        //根据这个id，查找它的所有上级
                                "label": "菜单1-2-1",
                            },
                            {
                                "id": 7,
                                "label": "菜单1-2-2",
                            },
                        ],
                    },
                ]
            },
            {
                "id": 7,
                "label": "菜单2",
                "children": [
                    {
                        "id": 8,
                        "label": "菜单2-1",
                        "children": [
                            {
                                "id": 9,
                                "label": "菜单2-1-1",
                            },
                            {
                                "id": 10,
                                "label": "菜单2-1-2",
                            }
                        ],
                    },
                ]
            }
        ]

        // 树形结构中查询是否有某个值，并返回他的全部父级和本身
        // 
        function findParent(data, target) {
            let result = []
            for (let i = 0; i < data.length; i++) {
                if (data[i].id === target) {
                    result.push(data[i].label)
                    return result
                }
                if (data[i].children) {
                    let temp = findParent(data[i].children, target)
                    if (temp) {
                        result.push(data[i].label)
                        return result.concat(temp)
                    }

                }
            }
        }
        console.log(findParent(data, 1))
        console.log(findParent(data, 10))
    </script>

</body>

</html>